'use strict';

const Service = require('egg').Service;

class UsersService extends Service {

  // 分页查询
  async findAndCountAll(opt){
    let { page, page_size } = opt;
    let { count, rows } = await this.ctx.model.Users.findAndCountAll({
      limit:page_size,
      offset:(page - 1) * page_size
    })

    return {
      current_page:parseInt(page),
      page_size:parseInt(page_size),
      total:count,
      data:rows
    }
  }

  // 查找全部方法
  async findAll(opt = ''){
    const users = await this.ctx.model.Users.findAll(opt);
    return users;
  }

  // 查找单个方法
  async findOne(opt = ''){
    return await this.ctx.model.Users.findOne(opt)
  }

  // 新建
  async create(opt){
    return await this.ctx.model.Users.create(opt)
  }

}

module.exports = UsersService;
